package com.fengmi.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: 施俊豪
 * Date: 2022/1/17 20:00
 * Note: Swagger配置类
 */
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
    private ApiInfo apiInfo() {
        // 配置swagger页面的信息
        Contact contact = new Contact("施俊豪", "http://www.wendaoxueyuan.com", "1964891156@qq.com");

        return new ApiInfoBuilder().title("风靡铺子运营平台接口文档")
                .description("风靡铺子运营平台接口文档描述")
                .version("1.1.0")
                .contact(contact)
                .build();
    }

    @Bean
    public Docket createRestApi(Environment environment) {

        //只有在dev环境或者sit环境下才会显示swagger
        Profiles of = Profiles.of("dev", "sit");
        boolean enable = environment.acceptsProfiles(of);

        // 添加head参数start
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        tokenPar.name("token")
                .description("令牌")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false).build();
        pars.add(tokenPar.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(enable)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.fengmi"))//这里写的是API所在接口的位置
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);
    }

}
